/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     hhttp://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.behavior
{

import flash.events.MouseEvent;
import flash.geom.Point;

import mx.core.ClassFactory;
import mx.core.IFactory;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;

import org.example.ui.component.dialog.HelpDialog;
import org.example.ui.core.IconPlacement;
import org.example.ui.skin.behavior.icon.IconTextInputLeftSkin;
import org.example.ui.skin.behavior.icon.IconTextInputRightSkin;

import spark.components.TextInput;

// example of inherited skinpart handler, the iconDisplay is declared in
// the IconBehavior class, so the handler needs to be set in the class
// meta header
[SkinPartHandler(id="iconDisplay", event="click", handler="iconDisplay_click")]

/**
 * 
 */
public class IconTextInputBehavior extends IconBehavior
{
	//--------------------------------------------------------------------------
	//
	//  ComponentAlias :: Varaibles
	//
	//--------------------------------------------------------------------------
	
	[ComponentAlias]
	
	/**
	 * The input host.
	 */
	public var input:TextInput;
	
	//--------------------------------------------------------------------------
	//
	//  Private :: Varaibles
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	private var helpPopUp:HelpDialog;
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */	
	public function IconTextInputBehavior()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Properted :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	override protected function defaultLabelPlacementFunction(placement:String):IFactory
	{
		var clazz:Class;
		
		switch(placement)
		{
			case IconPlacement.RIGHT:
				clazz = IconTextInputRightSkin;
				break;
			//case IconPlacement.LEFT:
			default:
				clazz = IconTextInputLeftSkin;
				break;
		}
		
		return new ClassFactory(clazz);
	}
	
	//--------------------------------------------------------------------------
	//
	//  skinpart_handler :: Handlers
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	skinpart_handler function iconDisplay_click(event:MouseEvent):void
	{
		if (helpPopUp)
			return;
		
		helpPopUp = new HelpDialog();
		helpPopUp.width = input.width;
		
		PopUpManager.addPopUp(helpPopUp, input);
		
		var help:String = input.getStyle("help");
		if (help != null)
			helpPopUp.textArea.text = help;		
		
		var point:Point = new Point(input.x, input.y);
		point = helpPopUp.parent.localToGlobal(point);
		
		helpPopUp.move(point.x, point.y + input.height);
		
		helpPopUp.systemManager.addEventListener(
			MouseEvent.MOUSE_DOWN, systemManager_mouseDownHandler);
		
		helpPopUp.dispatchEvent(new FlexEvent(FlexEvent.SHOW));
	}
	
	//--------------------------------------------------------------------------
	//
	//  Private :: Handlers
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	private function systemManager_mouseDownHandler(event:MouseEvent):void
	{
		helpPopUp.systemManager.removeEventListener(
			MouseEvent.MOUSE_DOWN, systemManager_mouseDownHandler);		
		PopUpManager.removePopUp(helpPopUp);
		helpPopUp = null;
	}
}
}